package com.example.prom.ok.y2023.work.maxmind;

import com.alibaba.fastjson.JSON;
import com.example.prom.listener.ExcelListener;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.AnonymousIpResponse;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.alibaba.excel.EasyExcelFactory.read;
import static com.alibaba.excel.EasyExcelFactory.write;

@Slf4j
public class MaxMindAnonymousIPHandler {

//    public static void main(String[] args) throws Exception{
//        List<MaxMindIpReaderVO2> maxMindIpReaderVOS = readExcel();
//        System.out.println("检测ip总数："+maxMindIpReaderVOS.size());
//        int i = 0;
//        for (MaxMindIpReaderVO2 maxMindIpReaderVO : maxMindIpReaderVOS) {
//            String client_ip = maxMindIpReaderVO.getClient_ip();
//            AnonymousIpResponse response = validateIP(client_ip);
//            if (response.isAnonymous() || response.isAnonymousVpn() || response.isHostingProvider() || response.isPublicProxy() || response.isResidentialProxy() || response.isTorExitNode()) {
//                System.out.println(response.toJson());
//                i++;
//            }
//        }
//        System.out.println("疑似使用匿名Ip数量：" + i);
//
//    }

    public static void main(String[] args) {
        AnonymousIpResponse response = validateIP("103.25.28.255");
        System.out.println(response.toString());
    }


    public static List<MaxMindIpReaderVO2> readExcel() {
        String fileName = "C:\\Users\\hp\\Desktop\\ok\\work\\gatewayBannedIps.xlsx";
        ExcelListener<MaxMindIpReaderVO2> excelListener = new ExcelListener<>();
        read(fileName, MaxMindIpReaderVO2.class, excelListener).sheet().doRead();
        List<MaxMindIpReaderVO2> dataList = excelListener.getDataList();
        return dataList;
    }


    public static AnonymousIpResponse validateIP(String ip) {
        AnonymousIpResponse response = null;
        File database = new File("C:\\Users\\hp\\Desktop\\ok\\work\\GeoIP2-Anonymous-IP_20230627\\GeoIP2-Anonymous-IP.mmdb");
        try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
            InetAddress ipAddress = InetAddress.getByName(ip);
            response = reader.anonymousIp(ipAddress);
//            System.out.println(response.isAnonymous()); // true
//            System.out.println(response.isAnonymousVpn()); // false
//            System.out.println(response.isHostingProvider()); // false
//            System.out.println(response.isPublicProxy()); // false
//            System.out.println(response.isResidentialProxy()); // false
//            System.out.println(response.isTorExitNode()); //true
        } catch (Exception e){
            log.error("ip解析异常,ip:{}", ip, e);
        }
        return response;
    }
}
